<template>
  <view class="profile">
    <image
      class="profile-header"
      src="/static/images/ucenter/header.jpg"
      mode="scaleToFill"
    />

    <view class="profile-form">
      <view class="profile-form-title">
        <text class="profile-title-text">个人信息</text>
      </view>

      <view class="avatar">
        <image
          src="/static/images/avatar.png"
          mode="scaleToFill"
        />
      </view>
      <view class="profile-form-input">
        <input
          type="text"
          placeholder="请输入账号"
          class="input"
          v-model="form.username"
        />
      </view>
      <view class="tips">注意：如不修改密码请勿填写</view>
      <view class="profile-form-input">
        <input
          type="password"
          placeholder="请输入密码"
          class="input"
          v-model="form.password"
        />
      </view>
      <view class="profile-form-input">
        <input
          type="password"
          placeholder="请再次输入密码"
          class="input"
          v-model="form.comform_password"
        />
      </view>
      <view class="profile-form-btn">
        <button
          class="btn btn-primary"
          @click="save"
        >
          保存
        </button>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name: "Login",
    data() {
      return {
        isLoginActive: true,
        form: {
          username: "",
          password: "",
          comform_password: "",
        },
      };
    },
    created() {
      if (this.user) {
        this.form.username = this.user.username;
      }
    },
    methods: {
      save() {
        if (this.form.password) {
          if (this.form.password === "") {
            console.log("password is empty");
            uni.showToast({
              title: "密码不能为空",
              icon: "none",
              duration: 3000,
            });
            return;
          }
          //检查密码长度
          if (this.form.password.length < 6) {
            console.log("password length is less than 6");
            uni.showToast({
              title: "密码长度不能少于6位",
              icon: "none",
              duration: 3000,
            });
            return;
          }
          if (this.form.password !== this.form.comform_password) {
            console.log("password not match");
            uni.showToast({
              title: "两次输入密码不一致",
              icon: "none",
              duration: 3000,
            });
            return;
          }
        }
        if (this.form.username === "") {
          console.log("username is empty");
          uni.showToast({
            title: "账号不能为空",
            icon: "none",
            duration: 3000,
          });
          return;
        }

        this.$store.commit("auth/setUser", {
          username: this.form.username,
          password: this.form.password || this.user.password,
        });
        uni.navigateBack({
          delta: 1,
        });
      },
      toLogin() {
        this.isLoginActive = true;
      },
      toRegister() {
        this.isLoginActive = false;
      },
    },
  };
</script>

<style lang="scss" scoped>
  .profile {
    display: flex;
    flex-direction: column;
    align-items: center;
    &-header {
      width: 750rpx;
      height: 500rpx;
    }

    &-form {
      position: absolute;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      top: 40%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 680rpx;
      background: rgba($color: #fff, $alpha: 0.85);
      padding: 30rpx;
      border-radius: $border-radius;
      box-shadow: $box-shadow;

      &-title {
        text-align: center;
        margin-bottom: 30rpx;
        font-size: 36rpx;
        font-weight: bold;
      }

      &-input {
        width: 100%;
        margin-bottom: 40rpx;
        border: 1px solid #ddd;
        border-radius: 40rpx;
        padding: 10rpx 30rpx;
        input {
          width: 100%;
          height: 50rpx;
          line-height: 50rpx;
          border: none;
          color: #333;
          background: none;
        }
      }

      &-btn {
        width: 100%;
        margin-bottom: 40rpx;
        text-align: center;
        &:last-child {
          margin-bottom: 0;
        }
        button {
          width: 80%;
        }
      }
    }

    .avatar {
      width: 120rpx;
      height: 120rpx;
      margin-bottom: 40rpx;
      border-radius: 60rpx;
      overflow: hidden;
      border: 8rpx solid #fff;
      image {
        width: 100%;
        height: 100%;
      }
    }

    .tips {
      text-align: center;
      color: #999;
      font-size: 24rpx;
      margin-bottom: 20rpx;
    }
  }
</style>
